package com.jc.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jc.order.common.DictionaryToJcConst;
import com.jc.order.constant.DictConst;
import com.jc.order.constant.OrderConst;
import com.jc.order.constant.ValidateErrorConst;
import com.jc.order.dto.*;
import com.jc.order.service.SystemDictDataService;
import com.jc.util.Result;
import com.jc.vo.PolicyRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 跨境保险校验策略
 */
@Slf4j
@Service("insuranceCrossValidateService")
public class InsuranceCrossValidateServiceImpl extends CargoValidateServiceImpl {


    @Autowired
    private SystemDictDataService systemDictDataService;


    @Override
    public Result validateRequest(PolicyRequest request, JcPolicyDTO dto) {
        Result r = new Result<>();
        List<String> errorList = new ArrayList<>();
        if (request != null) {
            validateCommonFields(request, dto, errorList);
        }
        if (errorList.isEmpty()) {
            r.ok(dto);
        } else {
            r.error().setContent(errorList);
        }
        return r;
    }

    @Override
    public Result validateDTO(JcPolicyDTO dto) {
        Result result = new Result<>();

        List<String> errorList = new ArrayList<>();

        super.deliverywayValueValid(errorList, dto);
        super.transportModeValid(errorList, dto);
        super.isShelfValid(errorList, dto);
        super.validateBaseAmountWay(errorList, dto);
        super.cargoValueValid(errorList, dto);
        super.currencyValid(errorList, dto);
        super.freightValid(errorList, dto);
        super.freightCurrencyValid(errorList, dto);

        if (!OrderConst.PREMIUM_CALCULATION.equalsIgnoreCase(dto.getLogic())) {
            super.getAttachment(dto);
            super.startTimeValid(errorList, dto);
            super.fromAddressValid(errorList, dto);
            super.toAddresssValid(errorList, dto);
            super.fromCountryValid(errorList, dto);
            super.transferCountryValid(errorList, dto);
            super.transferAddressValid(errorList, dto);
            super.toCountryValid(errorList, dto);
            super.insuredValid(errorList, dto);
            super.destTypeValid(errorList, dto);
            super.expressCompanyValid(errorList, dto);
            this.trackingNoValid(errorList, dto);
            super.chargeableWeight(errorList, dto);
            super.blnameValid(errorList, dto);
            super.waybillNumberValid(errorList, dto);
            super.packingValid(errorList, dto);
            super.packingQuantityValid(errorList, dto);
            super.cargoDescValid(errorList, dto);
            this.cargoCategoryValid(errorList, dto);

        }


        if (errorList.isEmpty()) {
            result.ok(dto);
        } else {
            result.error().setContent(errorList);
        }
        return result;
    }

    /**
     * 映射数据
     */
    private void validateCommonFields(PolicyRequest request, JcPolicyDTO p, List<String> errorList) {
        if (request != null) {
            super.validateTransportType(request, p, errorList);
            super.validateDestType(request, p, errorList);
            this.validateGoodsType(request, p, errorList);
            super.validateGoodsPackage(request, p, errorList);
            super.validateShelfGuarantee(request, p, errorList);
            super.validateDispatchType(request, p, errorList);
        }
    }

    /**
     * 原单号校验
     */
    private void trackingNoValid(List<String> errorList, JcPolicyDTO p) {
        String orderNumber = p.getOrderNumber();
        if (StrUtil.isEmpty(orderNumber)) {
            errorList.add(ValidateErrorConst.ERROR_ORDER_NUMBER_EMPTY);
        } else {
            super.trackingNoLengthValid(errorList, p);
        }
    }


    /**
     * 货物类别校验
     */
    private void cargoCategoryValid(List<String> errorList, JcPolicyDTO p) {
        Integer goodsType = p.getGoodsType();
        if (goodsType == null) {
            errorList.add(ValidateErrorConst.ERROR_CARGO_CATEGORY_EMPTY);
        } else {
            SystemDictDataDTO dictData = systemDictDataService.selectDictDataByDictTypeAndValue(DictConst.DICT_GOODS_TYPE, goodsType.toString());
            if (dictData == null) {
                errorList.add(ValidateErrorConst.ERROR_CARGO_CATEGORY_MISMATCH);
            }
        }
    }


    // 货物类型校验
    private void validateGoodsType(PolicyRequest request, JcPolicyDTO dto, List<String> errorList) {
        if (StrUtil.isNotBlank(request.getCargoCategoryCode())) {
            Object value = processPackingCode(request.getCargoCategoryCode(), DictionaryToJcConst.GOODSTYPE_MAP, errorList, "货物类别");
            if (value != null) {
                dto.setGoodsType(Integer.valueOf(value.toString()));
            }
        }
    }


}